import { ConditionService } from './../../service/conditionService';
import { Template } from './../../domain/models/template';
import { Guid } from './../../common/utils/guid';
import { ApiDataContext } from './../../domain/apiDataContext';
import { Transaction } from 'tiny-entity2';
import { TemplateService } from './../../service/templateService';
import { ServiceManager } from './../../common/serviceFactory/serviceManager';
import * as Koa from 'koa';
import { route, RouteOptionsVerify, UsingLock } from "../../common";
import { ServiceException } from '../../common/excpetion';
const tmpSvr = ServiceManager.Current.GetService<TemplateService>('TemplateService');
const conditionSvr = ServiceManager.Current.GetService<ConditionService>('ConditionService');
class TemplateController {
    @route(new RouteOptionsVerify('GET', '/api/template'))
    async queryTemplates(ctx: Koa.Context, next) {
        let q = ctx.query;
        let orgId = ctx.headers['orgid'];
        q || (q = {});
        if (!q.orgId) {
            q.orgId = orgId;
        }
        ctx.response.body = await tmpSvr.QueryTemplate(q);
        await next();
    }
    @route(new RouteOptionsVerify('GET', '/api/template/:id'))
    async getTemplateInfoById(ctx: Koa.Context, next) {
        let { id } = ctx.params;
        ctx.response.body = await tmpSvr.GetTemplateById(id);
        await next();
    }
    @route(new RouteOptionsVerify('POST', '/api/template'))
    async saveTemplate(ctx: Koa.Context, next) {
        let orgId = ctx.headers['orgid'];
        let data: Template = ctx.request.body;
        if (!data.id) {
            throw new ServiceException('未传入id');
        }

        await UsingLock(data.id, async () => {
            await Transaction(new ApiDataContext(), async ctx => {
                let tmp = await tmpSvr.GetTemplateById(data.id);
                if (tmp) {
                    await tmpSvr.DeleteTmpComItemByTemplate(data.id, ctx);
                    await tmpSvr.UpdateTemplate(data, ctx);
                }
                else {
                    if (!data.orgId) {
                        data.orgId = 'global';
                    }
                    data.createTime = Date.now();
                    await tmpSvr.CreateTemplate(data, ctx);
                }

                for (let item of data.tmpComItems) {
                    item.id = Guid.GetGuid();
                    item.templateId = data.id;
                    await tmpSvr.CreateTmpComItem(item, ctx);

                    if (item.condition) {
                        item.condition.id = Guid.GetGuid();
                        item.condition.objectId = item.id;
                        item.condition.objectType = 'TmpComItem';
                        await conditionSvr.CreateCondition(item.condition, ctx);
                    }
                }

                return data;
            });
        });

        ctx.response.body = data;
        await next();
    }
    @route(new RouteOptionsVerify('PUT', '/api/template'))
    async updateTemplate(ctx: Koa.Context, next) {
        ctx.response.body = null;
        await next();
    }
    @route(new RouteOptionsVerify('DELETE', '/api/template/:id'))
    async deleteTemplate(ctx: Koa.Context, next) {
        let { id } = ctx.params;

        ctx.response.body = await tmpSvr.DeleteTemplate(id);
        await next();
    }

    @route(new RouteOptionsVerify('GET', '/api/template/with/menuName'))
    async queryTemplatesWithMenuName(ctx: Koa.Context, next) {
        let q = ctx.query;
        let orgId = ctx.headers['orgid'];
        q || (q = {});
        if (!q.orgId) {
            q.orgId = orgId;
        }
        ctx.response.body = await tmpSvr.QueryTemplateWithMenuName(q);
        await next();
    }
}